*********************************************************************************
/*    Create scatter plot for revenue, cost, and revenue/cost by income		*/
*********************************************************************************
clear all

* File paths
local datapath "${mvpf_audits}/data/cleaned"
local graphpath "${graphpath}" 

* Version of fig to produce 
global version  ${version} // paper or slides

* Other formatting
if "${version}" == "slides" {
	global title "title(" ", size(vhuge))"
	global img wmf
	graph set window fontface "Arial"
	global title "title(" ", size(vhuge))"
	global axis_size "" //xsize(6.5) ysize(4)" // makes figure wider
}

else if "${version}" == "paper" {
	global title "title("")"
	global img pdf
	graph set window fontface "LMRoman10-Regular"
	global title " "
	global axis_size " " // makes figure wider
}

use "${mvpf_audits}/data/figures_rep/figure_2_5.dta", clear

/*------------------------------------------------------------------------------
Figure: Total revenue and costs by income percentile 
------------------------------------------------------------------------------*/
	
	* set y-axis label, title, and figure name
	local ylab = "Dollars per Audit ($)"
	lab val ${inc}_pctl ${inc}_pctl_label
	
	* xtitle
	if "${inc}"=="agi" global xtitle = "AGI percentile group"
	if "${inc}"=="tpi" global xtitle = "TPI percentile group"

	* Preferences 
	local symbsize small
	local textsize vsmall
	
	* Set fig range (y-axis) for each audit type (ops and corr)
	replace rev_tot_disc= 50000 if rev_tot_disc>50000
	local lb = 0
	local ub = 50000 // round(`max', 100)
	local int = 10000 // (`ub' - `lb')/5	
	global ylab "ylabel(0 "0" 10000 "10,000" 20000 "20,000" 30000 "30,000" 40000 "40,000" 50000 "95,500", nogrid format(%9.0fc))"
	
	*** Subgroup figure
	local fig_name = "scatter_costs_rev_${inc}"
		
	local lb_f : di %02.1f `lb'
	local ub_f : di %02.1f `ub'

	tw  (scatter rev_tot_disc ${inc}_pctl if inrange(${inc}_pctl, 1, 22), ///
		mcolor(sea) msymbol(C) msize(`symbsize')) ///
		(scatter costs_tot_future ${inc}_pctl if inrange(${inc}_pctl, 1, 22) , ///
		mcolor(cranberry) msymbol(T) msize(`symbsize')) ///
		, ///
		yscale(r(`lb_f' `ub_f')) ///
		ytitle("`ylab'") xtitle("${xtitle}")   ///
		///legend(order(1 2 )  lab(1 "Revenue") lab(2 "Total costs") ///
		///ring(0) pos(11) col(1) symxsize(*3.5) region(lcolor(gs11))) ///
		legend(off) ///
		${title} ${axis_size} ${ylab} ///
		xscale(r(1 22))  ///
		xlabel(1(1)22, value labsize(small) tl(.5) nogrid angle(45) )  
	graph export "`graphpath'/`fig_name'_`hetlab'.wmf", as(wmf) replace

/*------------------------------------------------------------------------------
Figure: Revenue/Cost by income percentile 
------------------------------------------------------------------------------*/	

local hetlab = "ops"
	
	* set y-axis label, title, and figure name
	local ylab = "Audit Revenue / Total Costs"
	lab val ${inc}_pctl ${inc}_pctl_label

	* Preferences 
	local symbsize small
	local textsize vsmall

	** Subgroup figure
	local fig_name = "scatter_rc_${inc}"
	
		* Set fig range (y-axis) for each audit type (ops and corr)
	if "`hetlab'"== "ops" {
		local lb = 0
		local ub = 7 // round(`max', 100)
		local int = 1 // (`ub' - `lb')/5	
		}
			
	if "`hetlab'"== "corr" {
		local lb = 0
		local ub = 12 // round(`max', 100)
		local int = 2 // (`ub' - `lb')/5	
		} 
		
	local lb_f : di %02.1f `lb'
	local ub_f : di %02.1f `ub'
	
	** xtitle
	if "`inc'"=="agi" global xtitle = "AGI percentile group"
	if "`inc'"=="tpi" global xtitle = "TPI percentile group"
	
	** Add Arrows pointing to markers at 99+
	summ rc_future_disc if ${inc}_pctl==22 
	local rc_999: di %4.1f r(mean)
	
	summ rc_future_disc if ${inc}_pctl==21 
	local rc_99: di %4.1f r(mean)

	
	** Rev/Labor Costs
	tw (scatter rc_future_disc ${inc}_pctl if inrange(${inc}_pctl, 1, 22) ,  ///
		 mcolor("${rc_col}") msymbol(S) msize(medsmall)) ///
		, ///
		yline(1, lw(thin) lp(dash) lc(gs11)) ///
		ylabel(`lb'(`int')`ub', nogrid format(%9.0fc)) yscale(r(`lb' `ub')) ///
		ytitle("`ylab'") xtitle("${xtitle}")   ///
		legend(off) ///
		${title} ${axis_size} ///
		xscale(r(1 22))  ///
		xlabel(1(1)22, value labsize(small) tl(.5) nogrid angle(45) )
	graph export "`graphpath'/`fig_name'_`hetlab'.wmf", as(wmf) replace
	
/*------------------------------------------------------------------------------
Figure: Revenue/Cost by income percentile with alternative OH multipliers
------------------------------------------------------------------------------*/	
* Rev/cost
	g rc2_low = rev_tot_disc/costs_tot_future
	g rc2_base = rev_tot_disc/(costs_tot_future*(3.93/5.3854))
	g rc2_high = rev_tot_disc/(costs_tot_future*(2.93/5.3854))
	
	*** Both figs
	* set y-axis label, title, and figure name
	local ylab = "Audit Revenue / Marginal Costs"
	lab val ${inc}_pctl ${inc}_pctl_label
	
	* Preferences 
	local symbsize small
	local textsize vsmall
	
	** Subgroup figure
	local fig_name = "scatter_rc_alt_Oh_${inc}_ops"
	
	local lb = 0
	local ub = 12 // round(`max', 100)
	local int = 3 // (`ub' - `lb')/5		
	local lb_f : di %02.1f `lb'
	local ub_f : di %02.1f `ub'
	
	** xtitle
	if "${inc}"=="agi" global xtitle = "AGI percentile group"
	if "${inc}"=="tpi" global xtitle = "TPI percentile group"
	
	summ rc2_base if ${inc}_pctl==22
	local rc_999: di %4.1f r(mean)
	
	summ rc2_base if ${inc}_pctl==21
	local rc_99: di %4.1f r(mean)
	

	
	** Rev/Direct Labor Costs
	tw  (scatter rc2_base ${inc}_pctl if inrange(${inc}_pctl, 1, 22),  ///
		 mcolor("${rc_col}") msymbol(S) msize(medsmall)) ///
		(rcap rc2_high rc2_low ${inc}_pctl if inrange(${inc}_pctl, 1, 22),  ///
		 lcolor("${rc_col}") ) ///
		, ///
		yline(1, lw(thin) lp(dash) lc(gs11)) ///
		ylabel(`lb'(`int')`ub', nogrid format(%9.0fc)) yscale(r(`lb' `ub')) ///
		ytitle("`ylab'") xtitle("${xtitle}")   ///
		legend(ring(0) pos(11) col(1) lab(1 "Baseline") lab(2 "Lower bound / upper bound") ///
		symxsize(*0.5) region(lcolor(gs11))) ///
		${title} ${axis_size} ///
		xscale(r(1 22))  ///
		xlabel(1(1)22, value labsize(small) tl(.5) nogrid angle(45) )  
	graph export "`graphpath'/`fig_name'.wmf", as(wmf) replace







